package com.zbkj.admin.task.mszs;


import com.beust.jcommander.internal.Lists;
import com.zbkj.common.constants.MszsConstants;
import com.zbkj.common.model.mszs.MszsOrder;
import com.zbkj.common.model.mszs.MszsRoom;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.mszs.MszsOrderService;
import com.zbkj.service.service.mszs.MszsRoomService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 根据订单离开时间-修改房间入住状态
 * @author lingfe
 */
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class RoomCheckInTask {

    @Resource
    private MszsOrderService orderService;
    @Resource
    private MszsRoomService roomService;

    /**
     * 每天12点执行
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void ordersTask() {

        //订单列表
        List<MszsOrder> orderList = orderService.ordersTask();

        //需修改入住状态房间列
        List<MszsOrder> orders = Lists.newArrayList();
        //需修改入住状态房间列
        List<MszsRoom> rooms = Lists.newArrayList();

        for (MszsOrder or:orderList){
            Date presentDate = DateUtil.nowDateTimeReturnDate("yyyy-MM-dd HH:mm:ss");//当前日期
            Date leavetDate = or.getLeaveTime(); //客人离场时间
            int result = leavetDate.compareTo(presentDate);
            if (result == 0 || result < 1) { //==0:日期相等，<1:leavetDate 晚于 presentDate

                MszsOrder order = new MszsOrder();
                order.setId(or.getId());
                order.setOrderStatus(MszsConstants.MSZS_ORDER_STATUS_STR_Completed);
                order.setOrderStatusNumber(MszsConstants.MSZS_ORDER_STATUS_INT_Completed);

                MszsRoom room = new MszsRoom();
                room.setId(or.getRoomId());
                room.setStatus(0);

                orders.add(order);
                rooms.add(room);
            }
        }

        roomService.upRoomStatus(rooms);
        orderService.updateStatus(orders);
    }

}
